﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml.Linq;

namespace RenRenClient
{
    public class RennClient
    {
        public delegate void MethodCallback(XDocument ar);

        public OAuth token { get; protected set; }

        public RennClient(OAuth oAuth)
        {
            token = oAuth;
        }

        /// <summary>
        /// 下载所有好友列表
        /// </summary>
        /// <param name="callback">下载成功的回调方法</param>
        public void BeginGetFriends(GetFriendCallback callback)
        {
            var methodcallback = new MethodCallback(delegate(XDocument xmlresult)
            {
                if (xmlresult != null)
                {
                    var friends = RennFriend.Parse(xmlresult);
                    callback(friends);
                }
            });
            CallMethod(methodcallback, "friends.getFriends", null);
        }

        /// <summary>
        /// 下载新鲜事
        /// </summary>
        /// <param name="types">新鲜事的类别，多个类型以逗号分隔</param>
        /// <param name="callback">下载成功的回调方法</param>
        public void BeginGetFeeds(string types, GetFeedCallback callback)
        {
            var methodcallback = new MethodCallback(delegate(XDocument xmlresult)
            {
                if (xmlresult != null)
                {
                    var feeds = RennFeed.Parse(xmlresult);
                    callback(feeds);
                }
            });

            APIParameterCollection paras = new APIParameterCollection();
            paras.Add("type", types);
            CallMethod(methodcallback, "feed.get", paras);
        }

        protected void CallMethod(MethodCallback methodcallback, string method, APIParameterCollection param)
        {
            APIParameterCollection paras = new APIParameterCollection();
            paras.Add("v", "1.0");
            paras.Add("method", method);
            paras.Add("access_token", token.AccessToken);
            paras.Add("format", "XML");
            if (param != null)
            {
                paras.AddRange(param);
            }

            string sig = CalSig(paras);
            paras.Add("sig", sig);

            string parameters = paras.Join("&");
            HttpWebRequest request = WebRequest.CreateHttp(RennURL.API);
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            using (var reqstream = new StreamWriter(request.GetRequestStream()))
            {
                reqstream.Write(parameters);
                reqstream.Close();
            }

            var callback = new AsyncCallback(delegate(IAsyncResult asynchronousResult)
                {
                    var response = request.EndGetResponse(asynchronousResult);
                    var respstream = response.GetResponseStream();
                    XDocument xmlresult = XDocument.Load(respstream);
                    methodcallback(xmlresult);
                });
            request.BeginGetResponse(callback, request);
        }

        protected string CalSig(APIParameterCollection paras)
        {
            string parastr = paras.Join("");
            parastr += token.SecretKey;
            return MD5Core.GetHashString(parastr);
        }
    }
}
